package com.nisco.ampf.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	public static final String DB_NAME = "ampf.db";
	public static final String DB_TABLE = "login";
	public static final int DB_VERSION = 1;

	public static final String KEY_ID = "_id";
	public static final String KEY_USERNAME = "username";
	public static final String KEY_PASSWORD = "password";

	public SQLiteDatabase db;
	public final Context context;
	public DBOpenHelper dbOpenHelper;

	public static class DBOpenHelper extends SQLiteOpenHelper {

		public DBOpenHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		// 建表语句
		private static final String DB_CREATE = "create table " + DB_TABLE
				+ "(" + KEY_ID + " integer primary key autoincrement, "
				+ KEY_USERNAME + " text not null, " + KEY_PASSWORD + " integer);";

		// 建表方法
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DB_CREATE);
			Log.i("DB", "表" + DB_TABLE + "新建成功！");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("drop table if exists " + DB_TABLE);
			Log.i("DB", "表" + DB_TABLE + "删除成功！");
			onCreate(db);
		}
	}

	public DBAdapter(Context _context) {
		context = _context;
	}

	/**
	 * 打开数据库
	 */
	public void Open() {
		dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
		try {
			db = dbOpenHelper.getWritableDatabase(); // Create and/or open a
														// database that will be
														// used for reading and
														// writing.
		} catch (SQLiteException e) {
			db = dbOpenHelper.getReadableDatabase(); // Create and/or open a
														// database.
		}
	}

	/**
	 * 向表中插入新数据
	 * 
	 * @param loginInfo
	 * @return
	 */
	public long insert(LoginInfo loginInfo) {
		ContentValues newValues = new ContentValues();
		newValues.put(KEY_USERNAME, loginInfo.userName);
		newValues.put(KEY_PASSWORD, loginInfo.passWord);

		return db.insert(DB_TABLE, null, newValues);

	}

	/**
	 * 删除表中所有数据
	 * 
	 * @return
	 */
	public long deleteAllData() {
		return db.delete(DB_TABLE, null, null);
	}

	/**
	 * 根据id删除一条数据
	 * 
	 * @param id
	 * @return
	 */
	public long deleteOneData(long id) {
		return db.delete(DB_TABLE, KEY_ID + "=" + id, null);
	}

	/**
	 * 查询所有数据
	 * 
	 * @return
	 */
	public LoginInfo[] queryAllData() {
		
		Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_USERNAME,
				KEY_PASSWORD}, null, null, null, null, null);
		return ConvertToLogonInfo(results);
		
	}

	/**
	 * 根据id查询数据
	 * 
	 * @param id
	 * @return
	 */
	public LoginInfo[] queryOneData(long id) {
		
			Cursor results = db.query(DB_TABLE, new String[] { KEY_ID,
					KEY_USERNAME, KEY_PASSWORD,}, KEY_ID + "=" + id, null,
					null, null, null);
			return ConvertToLogonInfo(results);
		
	}

	/**
	 * 根据id更新一条数据
	 * 
	 * @param id
	 * @param loginInfo
	 * @return
	 */
	public long updateOneData(long id, LoginInfo loginInfo) {
		ContentValues newValues = new ContentValues();
		newValues.put(KEY_USERNAME, loginInfo.userName);
		newValues.put(KEY_PASSWORD, loginInfo.passWord);

		return db.update(DB_TABLE, newValues, KEY_ID + "=" + id, null);
	}

	/**
	 * 将查询结果转换为用来存储数据自定义的People类对象
	 * 
	 * @param cursor
	 * @return
	 */
	private LoginInfo[] ConvertToLogonInfo(Cursor cursor) {
		int result = cursor.getCount();
		Log.i("DB", result + "");
		if (result == 0 || !cursor.moveToFirst()) {
			return null;
		}
		LoginInfo[] loginInfos = new LoginInfo[result];
		for (int i = 0; i < result; i++) {
			loginInfos[i] = new LoginInfo();
			loginInfos[i].ID = cursor.getInt(0);
			loginInfos[i].userName = cursor.getString(cursor.getColumnIndex(KEY_USERNAME));
			loginInfos[i].passWord = cursor.getString(cursor.getColumnIndex(KEY_PASSWORD));
			
			cursor.moveToNext();
		}
		return loginInfos;

	}

	/**
	 * 关闭数据库
	 */
	public void close() {
		if (db != null) {
			db.close();
			db = null;
		}
	}
}
